﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;

namespace TA.Turbo.Controls
{
	/// <summary>
	/// Implements members common to all complex "input" list controls (such as RadioButtonList and CheckBoxList).
	/// </summary>
	public static class ComplexInputListControlCommon
	{
		/// <summary>
		/// Props.
		/// </summary>
		public readonly static BasePropertyDefinition[] Properties =
			new BasePropertyDefinition[] { 
				new EnumPropertyDefinition<Orientation>("RepeatDirection", "RD", Orientation.Horizontal),
			};
		/// <summary>
		/// Renders an item group's begin tag.
		/// </summary>
		/// <param name="listControl"></param>
		/// <param name="writer"></param>
		/// <param name="item"></param>
		internal static void RenderItemGroupBeginTag(IComplexInputListControl listControl, HtmlTextWriter writer, ListItem item)
		{
			writer.AddAttribute(HtmlTextWriterAttribute.Class, "list_group");
			switch (listControl.RepeatDirection)
			{
				case System.Web.UI.WebControls.Orientation.Horizontal:
					writer.RenderBeginTag(HtmlTextWriterTag.Span);
					break;
				case System.Web.UI.WebControls.Orientation.Vertical:
					writer.RenderBeginTag(HtmlTextWriterTag.Div);
					break;
			}
		}
		/// <summary>
		/// Renders a complex input list control's option item.
		/// </summary>
		/// <typeparam name="T"></typeparam>
		/// <param name="listControl"></param>
		/// <param name="writer"></param>
		/// <param name="item"></param>
		public static void RenderItemOption<T>(T listControl, HtmlTextWriter writer, ListItem item)
			where T : BaseComplexListControl, IComplexInputListControl
		{
			writer.RenderBeginTag(HtmlTextWriterTag.Label);

			writer.AddAttribute(HtmlTextWriterAttribute.Name, listControl.UniqueID);
			writer.AddAttribute(HtmlTextWriterAttribute.Value, item.Value);

			if (listControl.ValueChanged.Active)
				writer.AddAttribute(HtmlTextWriterAttribute.Onchange, listControl.ValueChanged.GetEventScript(null));

			listControl.RenderItemOptionInternal(writer, item);

			writer.Write(item.Text);
			writer.RenderEndTag();

			if (listControl.RepeatDirection == Orientation.Vertical)
			{
				writer.Write("<br/>");
			}
		}
		/// <summary>
		/// Renders an option item's begin tag.
		/// </summary>
		/// <param name="writer"></param>
		/// <param name="item"></param>
		/// <param name="listControl"></param>
		internal static void RenderItemItemBeginTag(IComplexInputListControl listControl, HtmlTextWriter writer, ListItem item)
		{
			switch (listControl.RepeatDirection)
			{
				case System.Web.UI.WebControls.Orientation.Horizontal:
					writer.RenderBeginTag(HtmlTextWriterTag.Span);
					break;
				case System.Web.UI.WebControls.Orientation.Vertical:
					writer.RenderBeginTag(HtmlTextWriterTag.Div);
					break;
			}
		}
		internal static void RegisterIncludes(IComplexInputListControl listControl, IncludeCollection includes)
		{
			includes.Add(Script.Orientation);
			includes.Add(Script.Element_PerformRecursive);
			includes.Add(new ScriptResourceInclude("TA.Turbo.Res.JS.ListControl.BaseComplexInputListControl.js", typeof(CheckBoxList)));
		}
	}
}
